package _07_design_linked_list

import (
	"bytes"
	"fmt"
)

// @author xsomnus_xiawenye
// @since 2020/1/3 23:51

type MyLinkedList struct {
	dummyNode *node
	size      int
}

type node struct {
	next *node
	ele  int
}

/** Initialize your data structure here. */
func Constructor() MyLinkedList {
	return MyLinkedList{
		dummyNode: &node{
			next: nil,
			ele:  0,
		},
		size: 0,
	}
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
func (this *MyLinkedList) Get(index int) int {
	if this.checkIndex(index) {
		tmp := this.dummyNode
		for i := 0; i < index; i++ {
			tmp = tmp.next
		}
		if tmp != nil {
			return tmp.ele
		}
	}
	return -1
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
func (this *MyLinkedList) AddAtHead(val int) {
	newNode := &node{
		next: this.dummyNode.next,
		ele:  val,
	}
	this.dummyNode.next = newNode
	this.size++
}

/** Append a node of value val to the last element of the linked list. */
func (this *MyLinkedList) AddAtTail(val int) {
	newNode := &node{
		next: nil,
		ele:  val,
	}
	lastNode := this.getNode(this.size - 1)
	lastNode.next = newNode
	this.size++
}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
func (this *MyLinkedList) AddAtIndex(index int, val int) {
	if this.checkIndex(index) {
		if index == 0 {
			this.AddAtHead(val)
		} else {
			prevNode := this.getNode(index - 1)
			prevNode.next = &node{
				next: prevNode.next,
				ele:  val,
			}
			this.size++
		}
	}
}

/** Delete the index-th node in the linked list, if the index is valid. */
func (this *MyLinkedList) DeleteAtIndex(index int) {
	if this.checkIndex(index) {
		if index == 0 {
			this.dummyNode.next = this.dummyNode.next.next
			this.size--
		} else {
			prevNode := this.getNode(index - 1)
			prevNode.next = prevNode.next.next
			this.size--
		}
	}
}

func (this *MyLinkedList) checkIndex(index int) bool {
	if index < 0 || index > this.size-1 {
		return false
	}
	return true
}

/** get index-th node  */
func (this *MyLinkedList) getNode(index int) *node {
	if this.checkIndex(index) {
		tmp := this.dummyNode
		if index == 0 {
			return tmp.next
		}
		for i := 0; i < index; i++ {
			tmp = tmp.next
		}
		return tmp
	}
	return nil
}

func (this *MyLinkedList) String() string {
	var buffer bytes.Buffer
	buffer.WriteString(fmt.Sprintf("MyLinkedList: size = %d", this.size))
	tmp := this.dummyNode
	buffer.WriteString("[nil->")
	for i := 0; i < this.size; i++ {
		tmp = tmp.next
		buffer.WriteString(fmt.Sprintf("%d->", tmp.ele))

	}
	buffer.WriteString("]")
	return buffer.String()
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * obj := Constructor();
 * param_1 := obj.Get(index);
 * obj.AddAtHead(val);
 * obj.AddAtTail(val);
 * obj.AddAtIndex(index,val);
 * obj.DeleteAtIndex(index);
 */
